package com.uam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uam.common.exception.CustomException;
import com.uam.db.mapper.AircraftMapper;
import com.uam.db.model.entity.Aircraft;
import com.uam.service.AircraftService;
import com.uam.utils.StringUtils;
import com.uam.utils.TokenUtils;
import com.uam.web.common.model.PageRes;
import com.uam.web.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author uam
 * @create 2023/12/1 1:07
 * @email uam@uam.com
 */
@Service
public class AircraftServiceImpl implements AircraftService {
    @Autowired
    private AircraftMapper aircraftMapper;

    @Override
    public Result add(HttpServletRequest request, Long airplaneId, String airplaneName, Integer maxEndurance,
                      Integer minAltitude, Integer maxAltitude, Integer maxSpeed, Integer avgSpeed,
                      String modelName, Integer flightInterval, Integer verticalAcceleration, Integer horizontalAcceleration) {
         String loginNameForToken = TokenUtils.getLoginNameForToken(request);

        Aircraft aircraft = new Aircraft();
        aircraft.setAvgSpeed(avgSpeed);
        aircraft.setAirplaneName(airplaneName);
        aircraft.setMaxEndurance(maxEndurance);
        aircraft.setMinAltitude(minAltitude);
        aircraft.setMaxAltitude(maxAltitude);
        aircraft.setMaxSpeed(maxSpeed);
        aircraft.setModelName(modelName);
        aircraft.setCreateLoginName(loginNameForToken);
        aircraft.setCreateTime(LocalDateTime.now());
        aircraft.setUpdateTime(LocalDateTime.now());
        aircraft.setFlightInterval(flightInterval);
        aircraft.setVerticalAcceleration(verticalAcceleration);
        aircraft.setHorizontalAcceleration(horizontalAcceleration);
        int insert = aircraftMapper.insert(aircraft);
        return Result.createSuccess("添加飞行器成功");
    }

    @Override
    public Result edit(HttpServletRequest request, Long airplaneId,
                       String airplaneName,
                       Integer maxEndurance,
                       Integer minAltitude,
                       Integer maxAltitude,
                       Integer maxSpeed,
                       Integer avgSpeed,
                       String modelName, Integer flightInterval, Integer verticalAcceleration, Integer horizontalAcceleration) {
        LambdaUpdateWrapper<Aircraft> aircraftLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        aircraftLambdaUpdateWrapper.eq(Aircraft::getAirplaneId, airplaneId);
        aircraftLambdaUpdateWrapper.set(StringUtils.isNotEmpty(airplaneName), Aircraft::getAirplaneName, airplaneName);
        aircraftLambdaUpdateWrapper.set(null != maxEndurance, Aircraft::getMaxEndurance, maxEndurance);
        aircraftLambdaUpdateWrapper.set(null != minAltitude, Aircraft::getMinAltitude, minAltitude);
        aircraftLambdaUpdateWrapper.set(null != maxAltitude, Aircraft::getMaxAltitude, maxAltitude);
        aircraftLambdaUpdateWrapper.set(null != maxSpeed, Aircraft::getMaxSpeed, maxSpeed);
        aircraftLambdaUpdateWrapper.set(null != avgSpeed, Aircraft::getAvgSpeed, avgSpeed);
        aircraftLambdaUpdateWrapper.set(null != flightInterval, Aircraft::getFlightInterval, flightInterval);
        aircraftLambdaUpdateWrapper.set(null != verticalAcceleration, Aircraft::getVerticalAcceleration, verticalAcceleration);
        aircraftLambdaUpdateWrapper.set(null != horizontalAcceleration, Aircraft::getHorizontalAcceleration, horizontalAcceleration);
        aircraftLambdaUpdateWrapper.set(StringUtils.isNotEmpty(modelName), Aircraft::getModelName, modelName);
        aircraftLambdaUpdateWrapper.set(Aircraft::getUpdateTime, LocalDateTime.now());

        int update = aircraftMapper.update(null, aircraftLambdaUpdateWrapper);
        if (update == 0) {
            throw new CustomException("编辑失败");
        }
        return Result.createSuccess("编辑成功");
    }

    @Override
    public Result delete(HttpServletRequest request, List<Long> airplaneIds) {
        LambdaUpdateWrapper<Aircraft> aircraftLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        aircraftLambdaUpdateWrapper.in(CollectionUtils.isNotEmpty(airplaneIds), Aircraft::getAirplaneId, airplaneIds);
        int delete = aircraftMapper.delete(aircraftLambdaUpdateWrapper);
        return Result.createSuccess("删除飞行器成功");
    }

    @Override
    public Result list(Integer pageNum, Integer pageSize, String airplaneName, String createBy, Integer airplaneId) {

        Page<Aircraft> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Aircraft> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq(StringUtils.isNotEmpty(airplaneName),"airplane_name",airplaneName);
        queryWrapper.eq(StringUtils.isNotEmpty(createBy),"create_login_name",createBy);
        queryWrapper.eq(-1L!=airplaneId,"airplane_id",airplaneId);

        Page<Aircraft> aircraftPage = aircraftMapper.selectPage(page, queryWrapper);
        PageRes<Aircraft> data = new PageRes<>(aircraftPage.getRecords(), aircraftPage.getTotal());
        return Result.createSuccess(data);
        //  return null;
    }
}
